using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using emp.Model;
using emp.IDAL;
using System.Collections;

namespace emp.SqlServerDAL
{
    public class TaskFailedLog : ITaskFailedLog
    {
        public IList getTaskFailedLogList(string date)
        {
            string SQL_SELECT_USER_LIST = "select TFL_ID,TFL_TBI_ID,convert(varchar(10),TFL_DATE,121),TFL_TASK_DESC from TASK_FAILED_LOG where 1=1 ";
            if (date != "")
            {
                SQL_SELECT_USER_LIST = SQL_SELECT_USER_LIST + date;
            }
            SQL_SELECT_USER_LIST = SQL_SELECT_USER_LIST + " order by TFL_DATE desc";
            IList taskfailedloglist = new ArrayList();
            SqlParameter[] parm = new SqlParameter[] { };
            //Execute a query to read the products
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_USER_LIST, parm))
            {
                while (rdr.Read())
                {
                    TaskFailedLogInfo tb =
                        new TaskFailedLogInfo(rdr.GetInt32(0), rdr.GetInt32(1), rdr.GetString(2), rdr.GetString(3));
                    taskfailedloglist.Add(tb);
                }
            }
            return taskfailedloglist;
        }

        public void insertTaskFailedLog(TaskFailedLogInfo tflinfo)
        {
            SqlParameter[] cellParms = null;
            string strTmp = "insert into TASK_FAILED_LOG (TFL_TBI_ID,TFL_DATE,TFL_TASK_DESC) values('" + tflinfo.TFL_TBI_ID + "',getdate(),'" + tflinfo.TFL_TASK_DESC + "')";
            if (!isalreadyinsert(tflinfo.TFL_TBI_ID))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_DTC_INV))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, strTmp, cellParms);
                            trans.Commit();

                        }
                        catch (SqlException err)
                        {
                            trans.Rollback();
                            System.Console.WriteLine(err.Message);
                        }
                    }
                }
            }
        }

        public TaskBasicInfo getTaskBasicInfobyid(int tss_id)
        {
            string SQL_SELECT_USER_LIST = "select tbi_id,tbi_value from TASK_BASIC_INFO where TBI_ID= " + tss_id;            
            TaskBasicInfo tb = null;
            SqlParameter[] parm = new SqlParameter[] { };
            //Execute a query to read the products
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_USER_LIST, parm))
            {
                while (rdr.Read())
                {
                    tb = new TaskBasicInfo(rdr.GetInt32(0), rdr.GetString(1));
                }
            }
            return tb;
        }

        bool isalreadyinsert(int tss_id)
        {
            bool returnvalue = false;
            string SQL_SELECT_TSS = "SELECT 1 FROM TASK_FAILED_LOG WHERE TFL_TBI_ID=" + tss_id + " AND convert(varchar(10),tfl_date,121)=convert(varchar(10),getdate(),121)";
            SqlParameter[] parm = new SqlParameter[] { };
            //Execute a query to read the products
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_TSS, parm))
            {
                while (rdr.Read())
                {
                    returnvalue = true;
                }
            }
            return returnvalue;
        }
    }
}
